#include "Collection_Span.h"
#include "Log.h"

// default constructor to satisfy compiler in a few places in the code.
Collection_Span::Collection_Span(Collection* items) {
    this->items = items;
}

Collection_Span::Collection_Span(const Item_Iterator& start,
                                 const Item_Iterator& end, int n,
                                 Collection* items) {
    this->start = start;
    this->end   = end;
    this->n     = n;
    this->items = items;
}

Collection_Span::append(Collection_Span& other) {
    items->move_block(*other, *this);

    // update the pointers and size.
    end = other.end;
    n += other.n;
}

void Collection_Span::write_IDs(std::ostream& os) const {
    LOG_ENTER(OBJECT,"Collection_Span::write_IDs()");

    os << "(";
    int i = 0;
    for (Item_Iterator iter = start; i < n; iter++, i++) {
        Item *p = *iter;
        os << " " << p->ID;
    }
    os << " )";
    os.flush();

    LOG_EXIT();
}

void Collection_Span::write(std::ostream& os) const {
    LOG_ENTER(OBJECT,"Collection_Span::write()");

    int i = 0;
    for (Item_Iterator iter = start; i < n;
         iter++, i++) {
        Item *p = *iter;
        os << "list[" << i << "] ID " << p->ID
           << " \"" << p->name << "\""
           << " (x y)=(" << p->x << " " << p->y << ") \""
           << p->label << "\"\n";
    }
    os.flush();

    LOG_EXIT();
}

